package com.bankcomm.ui.specialwidget;

//import com.grid.test.LineGridActivity;
//import com.grid.test.R;
//import com.grid.test.LineGridActivity.ViewHolder;

import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BOC_SpecialShop_main_Activity;
import com.bankcomm.ui.adapter.DistenceAdaper;
import com.rytong.tools.datastorage.PackageManagerAndroid;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DistanceAndDistPicker extends LinearLayout implements
		OnClickListener {

	private ImageView Radiobut;
	private ImageView Radiobut2;
	private ImageView Radiobut3;
	private ImageView Radiobut4;
	private ImageView Radiobut5;
	LPMid mid_;
	public ImageView defaultRadiobutView;
	public MyBoderGridView boderGrid;
	public ImageView[] RadiobutImageViewArr;
	private TextView textR500, textR1000, textR1500, textR2000,textRAll;
	private TextView[] textRArr;
	public String[] text;
	public String [] Range={"500","1000","2000","5000"};

	public DistanceAndDistPicker(Context context, final String[] texts,
			String[] r) {
		super(context);
        text=texts;
		LayoutInflater.from(context).inflate(
				R.layout.distance_dist_picker_layout,
				DistanceAndDistPicker.this);
		Radiobut = (ImageView) this.findViewById(R.id.IMGview1);
		Radiobut2 = (ImageView) this.findViewById(R.id.IMGview2);
		Radiobut3 = (ImageView) this.findViewById(R.id.IMGview3);
		Radiobut4 = (ImageView) this.findViewById(R.id.IMGview4);
		Radiobut5 = (ImageView) this.findViewById(R.id.IMGview5);
		textR500 = (TextView) findViewById(R.id.TXTview1);
		textR1000 = (TextView) findViewById(R.id.TXTview2);
		textR1500 = (TextView) findViewById(R.id.TXTview3);
		textR2000 = (TextView) findViewById(R.id.TXTview4);
		textRAll = (TextView) findViewById(R.id.TXTview5);

		RadiobutImageViewArr = new ImageView[] { Radiobut, Radiobut2,
				Radiobut3, Radiobut4,Radiobut5 };
		textRArr = new TextView[] { textR500, textR1000, textR1500, textR2000 };
		
//		for (int i = 0; i < textRArr.length; i++) {
//			textRArr[i].setText(r[i]);
//		}

		Radiobut.setOnClickListener(this);
		Radiobut2.setOnClickListener(this);
		Radiobut3.setOnClickListener(this);
		Radiobut4.setOnClickListener(this);
		Radiobut5.setOnClickListener(this);
		mid_ = LPMid.getInstance();
		defaultradiobut();

		boderGrid = (MyBoderGridView) this.findViewById(R.id.areaselect);
		boderGrid.setColumns(3);
		if (texts != null) {
			boderGrid.setAdapter(new DistenceAdaper(this.getContext(), texts));
		} else {
			boderGrid.setVisibility(View.GONE);
		}
		boderGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				Activity act = (Activity) getContext();
				if (act instanceof BOC_SpecialShop_main_Activity) {
					String dist = texts[position];
					if (dist != null && ("所有").equals(dist)) {
						dist = "";
					((BOC_SpecialShop_main_Activity) act).aroundShopRangeText.setText("所有商圈");
					}else{
					((BOC_SpecialShop_main_Activity) act).aroundShopRangeText.setText(dist);
					}
					((BOC_SpecialShop_main_Activity) act).aroundShopDist_S = dist;

					String text = ((BOC_SpecialShop_main_Activity) act).shopSearchText
							.getText().toString();
					
					 PackageManagerAndroid.ANDROIDDB.save("dist_value",dist);
					
					((BOC_SpecialShop_main_Activity) act).sendSearchList(text,
							false);
					PopupWindow aroundPW = ((BOC_SpecialShop_main_Activity) act).aroundPW;
					LPMid.getInstance().isSB = false;
					if (aroundPW != null) {
						if (aroundPW.isShowing()) {
							aroundPW.dismiss();
						}
					}
				}
			}
		});
	}

	// 默认选中Radiobutview
	public void defaultradiobut() {

		int defaultRadiobutv = mid_.defaultRadiobut;
		for (int i = 0; i < RadiobutImageViewArr.length; i++) {
			if (defaultRadiobutv == i){
				RadiobutImageViewArr[i].setSelected(true);
			}else{
				RadiobutImageViewArr[i].setSelected(false);
			}
		}
		if(defaultRadiobutv!=4){
			if(boderGrid!=null)
			boderGrid.setVisibility(View.GONE);
		}else{
			if(boderGrid!=null)
		    boderGrid.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View viewv) {
		LPMid.getInstance().isSB = true;
		int t = viewv.getId();
		int id = 0;
		for (int i = 0; i < RadiobutImageViewArr.length; i++) {
			if (RadiobutImageViewArr[i].getId() == t) {
				id = i;
				viewv.setSelected(true);
				mid_.defaultRadiobut=i;
			} else {
				RadiobutImageViewArr[i].setSelected(false);
			}
			
		}
		if(id==4){
			Activity act = (Activity) getContext();
//			((BOC_SpecialShop_main_Activity) act).aroundShopRangeText
//			.setText("所有商圈");
			
			if (text == null) {
				((BOC_SpecialShop_main_Activity) act).needLocation();
			}else{
				boderGrid.setVisibility(View.VISIBLE);
			}
			return;
		}else{
			boderGrid.setVisibility(View.GONE);
		}
		Utils.printOutToConsole(textRArr[id].getText());
		Activity act = (Activity) getContext();
		if (act instanceof BOC_SpecialShop_main_Activity) {
			String range = textRArr[id].getText().toString();
			((BOC_SpecialShop_main_Activity) act).showRange=range;
			((BOC_SpecialShop_main_Activity) act).aroundShopRange_S = Range[id];
			((BOC_SpecialShop_main_Activity) act)
					.needLocation();
//			((BOC_SpecialShop_main_Activity) act).aroundShopRangeText
//					.setText("身边"+range+"m");
			PopupWindow aroundPW = ((BOC_SpecialShop_main_Activity) act).aroundPW;
			if (aroundPW != null) {
				if (aroundPW.isShowing()) {
					aroundPW.dismiss();
				}
			}
		}
	}

}
